import {IS_DEBUG} from '@/env'

function _showModal(data) {
	uni.showModal({
		title: '提示',
		mask: true,
		content: data.errmsg,
		showCancel: false,
		success: () => {
			uni.navigateToMiniProgram({
				appId: 'wx577a9056ab443c4b',
				success: () => {
					console.log('success')
					setTimeout(() => {
						_showModal(data)
					}, 300)
				},
				fail: () => {
					console.log('fail')
					_showModal(data)
				}
			})
		}
	})
}

export default {
    doRequest(method, url, data,contentType,type=false) {
		if(IS_DEBUG){
			var apiurl="https://teststate.renruikeji.cn/api/vs/" //测试
		}else{
			// var apiurl="https://state.renkangshangcheng.cn/api/vs/" //正式
			var apiurl="https://state.renruikeji.cn/api/vs/" //正式
		}
		var token=uni.getStorageSync('token');
		if(type){
			uni.showLoading({
				title:"加载中"
			})
		}
        return new Promise((resolve, reject) => {
			let header = {}
			if(contentType === 'json'){
				header = {
				    'content-type': 'application/json',
					'Authorization':token,
				}
			}else{
				header = {
				    'content-type': 'application/x-www-form-urlencoded',
					'Authorization':token,
				}
			}
            uni.request({
                method: method,
                url:apiurl+url,
                data,
                // data:data
                header: header,
                success: function(result) {
					uni.hideLoading();
					if(result.data.code === 1003) {
						_showModal(result.data.data)
						return false;
					}
					if(result.data.code!== 0 && type){
						uni.showToast({
							title:result.data.message,
							icon:'none'
						})
					}
                    // resolve调用后，即可传递到调用方使用then或者async+await同步方式进行处理逻辑
                    resolve(result)
                },
                fail: function(e) {
                    console.log('出错了')
                    // reject调用后，即可传递到调用方使用catch或者async+await同步方式进行处理逻辑
                    reject(e)
                },
            })
        })

    },

}
